package com.grm.controller;

import com.grm.common.Result;
import com.grm.service.LdapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * ldap控制器
 *
 * 关于windows的ad域控
 *      帐号就分两种userprincipalname和sAMAccountName
 *          userprincipalname的风格---name@area.com
 *          sAMAccountName的风格--name
 *  不要被windows上那个显示“area\name”蛊惑，你验证的时候就填写name就行了。
 *
 *  至于base就是--dc=area，dc=com
 *
 *  ldapTemplate.setIgnorePartialResultException(true);这就配置千万别忘了，不然验证时报异常的。
 *
 * @author gaorimao
 * @date 2022/02/25
 */
@Slf4j
@RestController
public class LdapController {
    @Autowired
    private LdapService ldapService;

    /**
     * ldap1
     *
     * @param username 格式： gaorimao@inspur.com
     * @param password 密码
     * @return {@link Result}
     */
    @RequestMapping(value = "/ldap1", method = RequestMethod.GET)
    public Result ldap1(@RequestParam String username, @RequestParam String password) {
        boolean res = ldapService.emailRequest(username, password);
        return Result.success("success",res);
    }

    /**
     * ldap2
     *
     * @param username 格式：gaorimao
     * @param password 密码
     * @return {@link Result}
     */
    @RequestMapping(value = "/ldap2", method = RequestMethod.GET)
    public Result ldap2(@RequestParam String username, @RequestParam String password) {
        boolean res = ldapService.requestLdap(username, password);
        return Result.success("success",res);
    }
}
